package light.mvc.controller.patient;


import javax.servlet.http.HttpServletRequest;

import light.mvc.controller.base.BaseController;
import light.mvc.page.admin.User;
import light.mvc.page.base.Grid;
import light.mvc.page.base.Json;
import light.mvc.page.base.PageFilter;
import light.mvc.page.base.SessionInfo;
import light.mvc.page.base.Status;
import light.mvc.page.patient.DoctorRegistration;
import light.mvc.service.admin.UserServiceI;
import light.mvc.service.patient.ReserveRegisterServiceI;
import light.mvc.utils.GlobalConstant;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/reserveRegister")
public class ReserveRegisterController extends BaseController {

	@Autowired
	private ReserveRegisterServiceI reserveRegisterService;
	
	@Autowired
	private UserServiceI userService;
	
	@RequestMapping("/manager")
	public String manager() {
		return "/register/reserveRegister";
	}
	
	@RequestMapping("/add")
	@ResponseBody
	public Json add(HttpServletRequest request,Integer doctorId,Long timestamp) {
		SessionInfo sessionInfo = (SessionInfo)request.getSession().getAttribute(GlobalConstant.SESSION_INFO);
		Integer userId = sessionInfo.getId();
		
		String errorMsg = null;
        int errorCode = -1;
        Json j = null;
		try {
			reserveRegisterService.add(userId,doctorId,timestamp);
			errorCode = 0;
			errorMsg = "增加成功";
			j = new Json(new Status(errorCode,errorMsg),null);
		} catch (Exception e) {
			errorMsg = e.getMessage();
			j = new Json(new Status(errorCode,errorMsg),null);
		}
        return j;
	}
	
	@RequestMapping("/addPage")
	public String detail(HttpServletRequest request, Integer doctorId) {
		DoctorRegistration doctorRegistration = reserveRegisterService.detail(doctorId);
		request.setAttribute("doctorRegistration", doctorRegistration);
		return "/register/reserveRegisterAdd";
	}
	
	@RequestMapping("/introductionPage")
	public String introductionPage(HttpServletRequest request, Integer doctorId) {
		User user = userService.get(doctorId);
		request.setAttribute("doctor",user);
		return "/register/reserveRegisterIntroduction";
	}
	
	@RequestMapping("/doctorDataGrid")
	@ResponseBody
	public Grid doctorDataGrid(User doctor, PageFilter ph) {
		Grid grid = new Grid();
		grid.setRows(userService.doctorGrid(doctor, ph));
		grid.setTotal(userService.doctorGridCount(doctor, ph));
		return grid;
	}

}
